<template>
<el-dialog :title="dialogTitle" :visible.sync="visible" width="30%" center append-to-body>
    <div class="dialog-body">
        <el-form ref="ruleForm" :model="ruleForm" label-position="top">
            <el-form-item label="备注" prop="remark" :required="type==2" :rules="remarkRules">
                <el-input type="textarea" v-model="ruleForm.remark" placeholder="请填写备注"></el-input>
            </el-form-item>

            <el-form-item label="附件">
                <upload-files ref="uploadFiles" v-model="ruleForm.image"></upload-files>
            </el-form-item>
        </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
    </div>
</el-dialog>
</template>

<script>
import uploadFiles from '@/components/upload/files.vue';

import {
    auditAcceptApi,
    auditRejectApi
} from '@/api/audit/index.js';

const ruleForm = {
    audit_flow_id: '',
    remark: '',
    image: [],
};
export default {
    name: "AuditHandle",
    components: {
        uploadFiles
    },
    inject: ['listVue'],
    data() {
        return {
            type: '',
            dialogTitle: '',
            visible: false,
            ruleForm: Object.assign({}, ruleForm),
        };
    },
    computed: {
        remarkRules: function() {
            if (this.type == 2) {
                return [{
                    required: true,
                    message: "请填写备注",
                }];
            } else {
                return [];
            }
        }
    },
    methods: {
        showDialog(flowId, type) {
            this.ruleForm = Object.assign({}, ruleForm);
            this.ruleForm.image = [];
            this.ruleForm.audit_flow_id = flowId;
            this.type = type;
            if (type == 1) {
                this.dialogTitle = '确定审核通过?';
            } else {
                this.dialogTitle = '确定审核驳回?';
            }
            this.visible = true;

            this.$nextTick(() => {
                this.$refs.uploadFiles.clearFiles();
            })
        },
        closeDialog() {
            this.visible = false;
        },
        submit() {
            let _this = this;
            this.$refs["ruleForm"].validate((valid) => {
                if (!valid) {
                    return false;
                }

                let data = Object.assign({}, _this.ruleForm);
                if (_this.type == 1) {
                    auditAcceptApi(data).then((res) => {
                        if (res.code == "200") {
                            _this.$message.success(res.msg);
                            this.$refs.uploadFiles.clearFiles();
                            _this.closeDialog();
                            _this.$emit("doRefresh");
                            this.listVue.getPageData();
                        } else {
                            _this.$message.warning(res.msg);
                        }
                    });
                } else {
                    auditRejectApi(data).then((res) => {
                        if (res.code == "200") {
                            _this.$message.success(res.msg);
                            _this.closeDialog();
                            this.$refs.uploadFiles.clearFiles();
                            _this.$emit("doRefresh");
                            this.listVue.getPageData();
                        } else {
                            _this.$message.warning(res.msg);
                        }
                    });
                }

            });
        }
    }
}
</script>
